package nio;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Copy {
	private static final int BUF_SIZE = 256 * 1024 - 1;

	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		FileInputStream fis = new FileInputStream(new File("data/data.bin"));
		FileOutputStream fos = new FileOutputStream(
				new File("data/nio_out.bin"));

		ByteBuffer buf = ByteBuffer.allocateDirect(BUF_SIZE);

		FileChannel fisCh = fis.getChannel();
		FileChannel fosCh = fos.getChannel();

		int size = BUF_SIZE + 1;
		while (size > 0) {
			buf.clear();
			size = fisCh.read(buf);
			buf.flip();
			if (size > 0) {
				while (buf.hasRemaining()) {
					fosCh.write(buf);
				}
			}
		}

		fisCh.close();
		fosCh.close();

		long end = System.currentTimeMillis();
		long delta = end - start;
		System.out.println("Total time (local copy NIO): " + delta + " ms.");
	}
}
